package com.android.sunflower.ui.flowerinfo

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.android.sunflower.R
import com.android.sunflower.databinding.FragmentFlowerInfoBinding
import com.android.sunflower.logic.room.entities.Flower
import com.android.sunflower.ui.plantlist.PlantListFragment
import com.android.sunflower.ui.plantlist.PlantListViewModel
import com.google.gson.Gson
import com.google.gson.GsonBuilder

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [FlowerInfo.newInstance] factory method to
 * create an instance of this fragment.
 */
class FlowerInfo : Fragment() {
    private val flowerInfoBinding get() =_flowerInfoBinding
    private var _flowerInfoBinding:FragmentFlowerInfoBinding?=null

    //与PlantListFragment共享同一个ViewModel
    private val viewModel=PlantListFragment.viewModel
    inner class MyHandler:Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if(msg.what==FlowerInfo.plantSuccess){
                Toast.makeText(activity,"种植成功!",Toast.LENGTH_SHORT).show()
            }
        }
    }
    companion object{
        const val plantSuccess=77
        var handler:Handler?=null
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handler=MyHandler()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _flowerInfoBinding=FragmentFlowerInfoBinding.inflate(inflater,container,false)
        return flowerInfoBinding?.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        flowerInfoBinding?.myGardenToolbar?.setNavigationOnClickListener {
            activity?.onBackPressed()
        }
        //获取传递过来的数据
        val position=arguments?.getInt("position")
        //给界面赋值
        if(position!=null){
            flowerInfoBinding?.apply {
                if(viewModel!=null){
                    flowerPic.setImageResource(viewModel.plantList.get(position).picture!!)
                    flowerName.text=viewModel.plantList[position].name
                    flowerInfo.text=viewModel.plantList[position].info
                    careInfo.text=viewModel.plantList[position].careInfo
                    waterGuide.text="浇水指南：每隔${viewModel.plantList[position!!].waterPeriod}天"
                }
            }
        }

        flowerInfoBinding?.addButton?.setOnClickListener {
            if(position!=null) viewModel?.addToMyGarden(viewModel.plantList[position])
        }

        //将花转换成json格式进行分享
        flowerInfoBinding?.myGardenToolbar?.setOnMenuItemClickListener { actionButton->
            val sharedContent=GsonBuilder().create().toJson(viewModel?.plantList?.get(position!!))
            when(actionButton.itemId){
                R.id.shareButton->{
                    val sendIntent=Intent().apply {
                        action=Intent.ACTION_SEND
                        putExtra(Intent.EXTRA_TEXT,sharedContent)
                        type="text/plain"
                    }
                    startActivity(Intent.createChooser(sendIntent,null))
                }
            }
            true
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _flowerInfoBinding=null
    }

    override fun onDestroy() {
        super.onDestroy()
        handler=null
    }
}